﻿using System;
using ZX.Common;
using ZX.DBModule;
using ZX.Web.UI.Controls;
using ZX.BLL.Extend;
using System.Collections.Generic;
using System.Collections;
using System.Text;
using ZX.Common.Extend;
using ZX.EHR.Library;

namespace ZX.WebApp.TR
{
    public partial class WorkCheck : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            AjaxPro.Utility.RegisterTypeForAjax(typeof(ZX.WebApp.TR.WorkCheck));
            string type=Request.QueryString["TYPE"];
            string id = Request.QueryString["WPID"];
            if (!this.IsPostBack)
            {
                banddata(type,id);
            }
        }

        //
        protected void banddata(string type,string id)
        {
            DatUser du = (DatUser)Session["User"];
            //周
            if (type == "W")
            {
                ZX.TR.Library.V_TrWeekCheck twc = new ZX.TR.Library.V_TrWeekCheck();
                BaseBussnies bwc = new BaseBussnies(twc);
                IList<ZX.TR.Library.V_TrWeekCheck> twcl = bwc.GetEntitys<ZX.TR.Library.V_TrWeekCheck>("TrWeekPlanID='" + id + "' AND ADDUSER='"+du.UserID+"'");
                if (twcl != null)
                {
                    hdfcheckid.Value = twcl[0].TrWeekCheckID.ToString();
                    TaCheck.Value = twcl[0].CheckContent;
                    LabAddTime.Text = twcl[0].AddTime.ToString("yyyy-MM-dd hh:mm:ss");
                    LabAddUser.Text = twcl[0].UName;
                    hdfAddUser.Value = twcl[0].AddUser.ToString();
                }
                else
                {
                    hdfcheckid.Value = Guid.NewGuid().ToString();
                    LabAddTime.Text = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss");
                    LabAddUser.Text = du.UName;
                    hdfAddUser.Value = du.UserID.ToString();
                }
                
            }
            else
            {
                ZX.TR.Library.V_TrDateCheck tdc = new ZX.TR.Library.V_TrDateCheck();
                BaseBussnies bdc = new BaseBussnies(tdc);
                IList<ZX.TR.Library.V_TrDateCheck> tdcl = bdc.GetEntitys<ZX.TR.Library.V_TrDateCheck>("TrDatePlanID='" + id + "' AND ADDUSER='" + du.UserID + "'");
                if (tdcl != null)
                {
                    hdfcheckid.Value = tdcl[0].TrDateCheckID.ToString();
                    TaCheck.Value = tdcl[0].CheckContent;
                    LabAddTime.Text = tdcl[0].AddTime.ToString("yyyy-MM-dd hh:mm:ss");
                    LabAddUser.Text = tdcl[0].UName;
                    hdfAddUser.Value = tdcl[0].AddUser.ToString();
                }
                else
                {
                    hdfcheckid.Value = Guid.NewGuid().ToString();
                    LabAddTime.Text = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss");
                    LabAddUser.Text = du.UName;
                    hdfAddUser.Value = du.UserID.ToString();
                }
            }
        }

        //检查人检测
        [AjaxPro.AjaxMethod]
        public int SaveCheck(string str)
        {
            string[] strs = str.Split('|');
            bool back = false;
            if (strs.Length > 1)
            {
                string type = strs[0];
                string id = strs[1];
                string cid = strs[4];
                if (type == "W")
                {
                    ZX.TR.Library.TrWeekCheck twc = new ZX.TR.Library.TrWeekCheck();
                    BaseBussnies bwu = new BaseBussnies(twc);
                    IList<ZX.TR.Library.TrWeekCheck> twcl = bwu.GetEntitys<ZX.TR.Library.TrWeekCheck>("TrWeekCheckID='" + cid + "'");
                    if (twcl != null)
                    {
                        twcl[0].CheckContent = strs[2];
                        BaseBussnies bwc = new BaseBussnies(twcl[0]);
                        back = bwc.Update();
                    }
                    else
                    {
                        twc.TrWeekCheckID = new Guid(cid);
                        twc.TrWeekPlanID = new Guid(id);
                        twc.CheckContent = strs[2];
                        twc.AddTime = DateTime.Now;
                        twc.AddUser = new Guid(strs[3]);
                        BaseBussnies bwc = new BaseBussnies(twc);
                        back = bwc.Insert();
                    }
                    
                }
                else
                {
                    ZX.TR.Library.TrDateCheck tdc = new ZX.TR.Library.TrDateCheck();
                    BaseBussnies bdu = new BaseBussnies(tdc);
                    IList<ZX.TR.Library.TrDateCheck> tdcl = bdu.GetEntitys<ZX.TR.Library.TrDateCheck>("TrDateCheckID='" + cid + "'");
                    if (tdcl != null)
                    {
                        tdcl[0].CheckContent = strs[2];
                        BaseBussnies bdc = new BaseBussnies(tdcl[0]);
                        back = bdc.Update();
                    }
                    else
                    {
                        tdc.TrDateCheckID = new Guid(cid);
                        tdc.TrDatePlanID = new Guid(id);
                        tdc.CheckContent = strs[2];
                        tdc.AddTime = DateTime.Now;
                        tdc.AddUser = new Guid(strs[3]);
                        BaseBussnies bdc = new BaseBussnies(tdc);
                        back = bdc.Insert();
                    }
                }
            }
            if (back)
                return 1;
            else
                return 0;
        }
    }
}